<?php

namespace app\common\model;

use think\Cache;
use think\Model;




class Storage extends Model{



    // 表名
    protected $name = 'order_info';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = false;

    // 定义时间戳字段名
    protected $createTime = false;
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
    ];

    public function getOrderIds($storage_ids){
        $where    = [];
        $where['oi_id'] = ['in',$storage_ids];

        return  static::alias('mdsi')
            ->where($where)
            ->field('oi_order_id')
            ->group('mdsi.oi_order_id')
            ->select();
    }

    /**
     * 表查询
     */
    public function getStorageInfoListBysYear($where){
        $where['oi_is_deleted'] = 0;
        return static::where($where)
            ->group('oi_cate_name,oi_year,oi_specs,oi_is_fear_query')
            ->field('*,count(*) as total')
            ->select();
    }


    public function getStorageInfoListGroupYear($where,$enterprise_id){
        $wherenew['oi_enterprise_id'] = $enterprise_id;
        $wherenew['oi_is_deleted'] = 0;
        $wherenew['o_is_deleted'] = 0;
        $wherenew['oi_surplus_num'] = ['>',0];
        $wherenew['oi_enterprise_id'] = $enterprise_id;
        $wherenew['oi_warehouse'] = 2;
        return static::alias('mdsi')
            ->where($wherenew)
            ->where($where)
            ->join('order o', 'mdsi.oi_order_id = o.o_id', 'left')
            ->order(['oi_create_time' => 'DESC' , 'oi_id'=> 'DESC'])
            ->group('oi_cate_id,oi_year,oi_specs,oi_is_fear_query')
            ->field('mdsi.*,sum(oi_surplus_num) as surplus_num,sum(oi_price * oi_surplus_num * oi_bottle_num) as price_total,sum(oi_bottle_num * oi_surplus_num) as surplus_bottle_num')
            ->select();
    }

    public function getProfit($type,$enterprise_id){
        $where_end = [];
        if($type == 'today'){
            $where['oi_create_time'] = ['>=',strtotime(date('Y-m-d',time()))];
        }
        if($type == 'yesterday'){
            $where['oi_create_time'] = ['<',strtotime(date('Y-m-d',time()))];
            $where_end['oi_create_time'] = ['>=',strtotime(date('Y-m-d',time())) - 86400];
        }
        if($type == 'thismonth'){
            $where['oi_create_time'] = ['>=',strtotime(date('Y-m',time()))];
        }
        if($type == 'lastmonth'){
            $where['oi_create_time'] = ['<',strtotime(date('Y-m',time()))];
            $where_end['oi_create_time'] = ['>=',mktime(0,0,0,date('m',time())-1,0,date('Y',time()))];
        }
        if($type == 'year'){
            $where['oi_create_time'] = ['>=',strtotime(date('Y-01-01',time()))];
        }
        $ret = $this->getProfitTotal($where,$enterprise_id,$where_end);
        return $ret;
    }

    /**
     * @param string $tid
     * @return mixed
     */
    public function getDeletedListByTid(string $tid){
        return static::where(['oi_number'=>$tid])->select();
    }

    public function getDeletedList($where){
        return static::where($where)->select();
    }


    public function getList($where){
        $where['oi_is_deleted'] = 0;
        return static::where($where)->select();
    }

    /**
     * 表查询 利润查询
     */
    public function getProfitTotal($where,$enterprise_id =0,$where_end = []){
        $where_end['oi_is_deleted']     = 0;
        $where_end['o_is_deleted']      = 0;
        $where_end['oi_warehouse']      = 1;
        $where_end['oi_purchase_price'] = ['>',0];
        $where_end['oi_price']          = ['>',0];
        $where_end['oi_enterprise_id'] = $enterprise_id;


        $data =  static::alias('mdsi')
            ->join('order o', 'mdsi.oi_order_id = o.o_id', 'left')
            ->where($where)
            ->where($where_end)
            ->field('sum((oi_price - oi_purchase_price)  * oi_num * oi_bottle_num) as profit_total')
            ->find();
        return $data['profit_total']??0;

    }


    public function getPriceTotal($where){
        $where['oi_is_deleted'] = 0;
        $where['o_is_deleted'] = 0;
        $data =  static::alias('mdsi')
            ->join('order o', 'mdsi.oi_order_id = o.o_id', 'left')
            ->where($where)
            ->field('sum(oi_price * oi_num * oi_bottle_num) as price_total')
            ->find();
        return $data['price_total']??0;
    }


    /**
     * 获取库存总价值
     * @param $where
     * @return int|mixed
     */
    public function getWarehoustMoney($where){
        $where['oi_is_deleted'] = 0;
        $where['o_is_deleted'] = 0;
        $data =  static::alias('mdsi')
            ->join('order o', 'mdsi.oi_order_id = o.o_id', 'left')
            ->where($where)
            ->field('sum(oi_price * oi_surplus_num * oi_bottle_num) as price_total')
            ->find();
        return $data['price_total']??0;
    }




    /**
     * 根据日期批次进行分组
     */
    public function getStorageInfoListByDate($where,$sort,$es_id){
        $wherenew['oi_is_deleted'] = 0;
        $wherenew['o_is_deleted'] = 0;
        $wherenew['oi_surplus_num'] = ['>',0];
        $wherenew['oi_date'] = ['<>',''];
        $wherenew['oi_warehouse'] = 2;
        $wherenew['oi_enterprise_id'] = $es_id;
        $data =  static::alias('mdsi')
            ->join('order o', 'mdsi.oi_order_id = o.o_id', 'left')
            ->join('mt_select_category cate', 'mdsi.oi_cate_id = cate.c_id', 'left')
            ->where($where)
            ->where($wherenew)
            ->order($sort)
            ->field('*,sum(oi_surplus_num * oi_bottle_num) as bottle_num')
            ->group('oi_date')
            ->select();
        return $data;
    }





    public function getMdoCreateTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['o_create_time']) ? $data['o_create_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getMdoUpdateTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['o_update_time']) ? $data['o_update_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }



    public function category()
    {
        return $this->belongsTo(\app\admin\model\select\Category::class, 'oi_cate_id', 'c_id', [], 'LEFT')->setEagerlyType(0);
    }



}